//
//  ShouYeViewController.m
//  XiangMu
//
//  Created by  方崇豪 on 15/7/13.
//  Copyright (c) 2015年  方崇豪. All rights reserved.
//

#import "ShouYeViewController.h"
#import "Header.h"
#import "UIButton+WebCache.h"
@interface ShouYeViewController ()

@end

@implementation ShouYeViewController

- (void)viewWillAppear:(BOOL)animated{
    
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    _imagesArr = [[NSMutableArray alloc] initWithCapacity:0];

    self.automaticallyAdjustsScrollViewInsets=NO;

    
    
    [self initNav];
    //网络监听
    [self WangLuoJianTing];
    
    [self QingQiuImags];
    
    //医院 社区 等等一些控件
    [self HuoDongImagView];
    [self hudPorogtrss];

    

    
}
//导航条按钮
- (void)initNav{
    UIColor * color = [UIColor whiteColor];
    NSDictionary * dict=[NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
    self.navigationController.navigationBar.titleTextAttributes = dict;
    
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"dianchiliang"] forBarMetrics:UIBarMetricsDefault];
    
    //杭州
    UILabel *hangZhou = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 35, 30)];
    hangZhou.textColor = [UIColor whiteColor];
    hangZhou.textAlignment = NSTextAlignmentLeft;
    
    hangZhou.text=@"杭州";
    UIBarButtonItem*barbutton = [[UIBarButtonItem alloc]initWithCustomView:hangZhou];
    
    
    //我的消息
    
    UIButton * woDeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    woDeBtn.frame=CGRectMake(kScreenW/2+120, 20, 20, 20);
    [woDeBtn addTarget:self action:@selector(WoDeXiaoXi) forControlEvents:UIControlEventTouchUpInside];
    
    [woDeBtn setBackgroundImage:[UIImage imageNamed:@"speech-bubble-3"] forState:UIControlStateNormal];
    UIBarButtonItem*barbutton3=[[UIBarButtonItem alloc]initWithCustomView:woDeBtn];
    self.navigationItem.rightBarButtonItem=barbutton3;
    
    
    //定位
    UIButton * dingWen = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [dingWen setBackgroundImage:[UIImage imageNamed:@"10"] forState:UIControlStateNormal];
    
    [dingWen addTarget:self action:@selector(XuanZeChengShi) forControlEvents:UIControlEventTouchUpInside];
    
    dingWen.frame = CGRectMake(hangZhou.frame.origin.x+kScreenW/8, 20, 12, 7);
    UIBarButtonItem*barbutton1=[[UIBarButtonItem alloc]initWithCustomView:dingWen];
    
    self.navigationItem.leftBarButtonItems=@[barbutton,barbutton1];

}
//滑动试图
- (void)pageView{
    
    PageView * page =[[PageView alloc]initPageViewFrame:CGRectMake(0, 0, kScreenW, kScreenH/3-10)];
    
    page.isWebImage = YES;
    page.imageArray =_imagesArr;
    page.duration = 2.0;
    
    [self.view addSubview:page];
    
}
//菜单按钮
- (void)HuoDongImagView{
   
    NSArray*titArr=[[NSArray alloc]initWithObjects:@"医院陪护", @"社区陪护", @"发布陪护", @"新手手册", @"陪护分享", nil];
     NSArray*imagArr=[[NSArray alloc]initWithObjects:@"yiyuan", @"shequ", @"fabu", @"shouce", @"fenxiang", nil];
  
    for (int i=0; i<3; i++)
    {  UIButton*yiYuanBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        UILabel*biaoTilable=[[UILabel alloc]init];
        biaoTilable.textAlignment=NSTextAlignmentCenter;
            yiYuanBtn.frame=CGRectMake(i%3*((kScreenW-20)/4+30)+30, kScreenH/3-10+10, (kScreenH-40)/9, (kScreenH-40)/9);
            
            [yiYuanBtn setBackgroundImage:[UIImage imageNamed:[imagArr objectAtIndex:i]]forState:UIControlStateNormal];
            
            biaoTilable.frame=CGRectMake(yiYuanBtn.frame.origin.x-10, yiYuanBtn.frame.origin.y+yiYuanBtn.frame.size.height+10, yiYuanBtn.frame.size.width+20, 20);
            biaoTilable.text=[titArr objectAtIndex:i];
        [biaoTilable setFont:[UIFont systemFontOfSize:12]];
        [self.view addSubview:biaoTilable];
        [self.view addSubview:yiYuanBtn];
        
        if (i<2) {
            
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(30+i*((kScreenW-20)/4+50)+45, biaoTilable.frame.origin.y+biaoTilable.frame.size.height+10,(kScreenH-40)/9, (kScreenH-40)/9);
            [btn setBackgroundImage:[UIImage imageNamed:[imagArr objectAtIndex:i+3]] forState:UIControlStateNormal];
            btn.tag=i+3;
             [btn addTarget:self action:@selector(CaiDanBtn:) forControlEvents:UIControlEventTouchUpInside];
           [self.view addSubview:btn];
            UILabel * lable1 = [[UILabel alloc]initWithFrame:CGRectMake(btn.frame.origin.x-10, btn.frame.origin.y+btn.frame.size.height+10, btn.frame.size.width+20, 20)];
            lable1.text = [titArr objectAtIndex:i+3];//             [
            [lable1 setFont:[UIFont systemFontOfSize:12]];
            lable1.textAlignment = NSTextAlignmentCenter;
            [self.view addSubview:lable1];
            if (i==0) {
                CGFloat  ihponeY;
                if (iphone4x_3_5) {
                    
                    ihponeY = 8;
                    
                }
                else if (iphone5x_4_0){
                    
                    ihponeY = 24;
                }
                else if (iphone6_4_7){
                     ihponeY = 40;
                }
                else if (iphone6Plus_5_5){
                    
                     ihponeY = 50;
                }
                UIImageView*dianHuaImage = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenW/2-105, lable1.frame.size.height+lable1.frame.origin.y+ihponeY, 20, 20)];
                dianHuaImage.image = [UIImage imageNamed:@"dianhua"];
                
                UIButton * dianHuaBtn = [UIButton buttonWithType:UIButtonTypeCustom];
                
                dianHuaBtn.frame = CGRectMake(kScreenW/2-100, dianHuaImage.frame.origin.y, 200, 20);
                [dianHuaBtn setTitle:KFDH forState: UIControlStateNormal];
                [dianHuaBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
                [dianHuaBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                [dianHuaBtn addTarget:self action:@selector(DianHua:) forControlEvents:UIControlEventTouchUpInside];
                
                [self.view addSubview:dianHuaBtn];
                
                [self.view addSubview:dianHuaImage];
                
            }
            
        

        }
        [yiYuanBtn addTarget:self action:@selector(CaiDanBtn:) forControlEvents:UIControlEventTouchUpInside];
        yiYuanBtn.tag=i;
        
        
    }
    
   
    
}
//客服电话
- (void)DianHua:(UIButton*)sender {
    
       UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@"您确定要拨打电话吗?" message:sender.titleLabel.text delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    
      [alert show];
    
}
#pragma mark ---UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    switch (buttonIndex) {
        case 0:
        {
          
            NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",HJKFDH];
         
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        }
            break;
        case 1:
        {
            
        }
            break;

        default:
            break;
    }
}
//菜单按钮触发方法
- (void)CaiDanBtn:(UIButton*)sender {
    
    switch (sender.tag) {
        case 0:
        {
            YiYuanPeiHuViewController*yiYuan=[[YiYuanPeiHuViewController alloc]init];
            yiYuan.tittexte=@"陪护信息";
            [self.navigationController pushViewController:yiYuan animated:YES];
            
        }
            break;
        case 1:
        {
            SheQuPeiHuViewController*s=[[SheQuPeiHuViewController alloc]init];
           
            
            [self.navigationController pushViewController:s animated:YES];
            
        }
            break;
        case 2:
        {
            FaBuPeiHu1ViewController*f=[[FaBuPeiHu1ViewController alloc]init];
            [self.navigationController pushViewController:f animated:YES];
            
        }
            break;
        case 3:
        {
            XinShouShouCeViewController*x=[[XinShouShouCeViewController alloc]init];
            [self.navigationController pushViewController:x animated:YES];
        }
            break;
        case 4:
        {
            PeiHuFenXingViewController*p=[[PeiHuFenXingViewController alloc]init];
            
            [self.navigationController pushViewController:p animated:YES];
        }
            break;
        default:
            break;
    }
}

#pragma mark--城市选择
- (void)XuanZeChengShi {
  
    XuanZeChengShiViewController*xuanze=[[XuanZeChengShiViewController alloc]init];
 
    [self.navigationController presentViewController:xuanze animated:YES completion:nil];
    
    
    
}
#pragma mark---我的消息
//我的消息
- (void)WoDeXiaoXi
{
    WoDeXiaoXiViewController*w=[[WoDeXiaoXiViewController alloc]init];
    [self.navigationController pushViewController:w animated:YES];
    
}
- (void)WangLuoJianTing{
    
   reacha =[Reachability reachabilityWithHostName:@"http://www.baidu.com"];
    [reacha startNotifier];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
}
-(void)reachabilityChanged:(NSNotification*)not{
    Reachability *rr =[not object];
    
    int status =[rr currentReachabilityStatus];
    
//    NSString *messageStr =nil;
    switch (status) {
        case NotReachable:{
//            NSLog(@"没网");
            
            [MBProgressHUD hideHUDForView:self.view animated:YES];
//            messageStr =@"网络连接错误请检查!";
            MBProgressHUD *hud =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
            
            hud.mode = MBProgressHUDModeText;
            
            hud.labelText = @"网络连接错误";
            
            hud.removeFromSuperViewOnHide = YES;
            
            [hud hide:YES afterDelay:1.5];

        }
            
            break;
        case ReachableViaWiFi:
   
//            messageStr =@"已连接wifi";
            break;
        case ReachableViaWWAN:
//            NSLog(@"2G/3g/4G");
//            messageStr =@"当前正在使用移动网络";
            break;
//            NSLog(@"有wang");
            
        default:
            break;
    }
//    [[[UIAlertView alloc]initWithTitle:messageStr message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]show];

}
-(void)hudPorogtrss{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    hud.labelText=@"数据加载中....";
    
    [hud show:YES];

}
//获取轮播图的iamge
- (void)QingQiuImags{
    
    [QingQiuShuJu ShouYeImagView:@"1" HuiChuan:^(NSMutableArray *arr, BOOL isOK) {
        
        if (isOK) {

            _imagesArr= arr;
            [MBProgressHUD hideHUDForView:self.view animated:YES];
                [self pageView];
            
        }
        else{
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            
            MBProgressHUD *hud =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
            
            hud.mode = MBProgressHUDModeText;
            
            hud.labelText = @"网络连接错误";
            
            hud.removeFromSuperViewOnHide = YES;
            
            [hud hide:YES afterDelay:1.5];
            

            
        }
    }];
}
- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
